package com.yijinsuo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yijinsuo.R;
import com.yijinsuo.base.BaseActivity;
import com.yijinsuo.entity.BankList;
import com.yijinsuo.http.HttpCallBack;
import com.yijinsuo.http.HttpUtils;
import com.yijinsuo.mian.Config;
import com.yijinsuo.response.BankCardRes;
import com.yijinsuo.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的银行卡
 */
public class MyBankCardActivity extends BaseActivity implements  SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.swipe_container)
    SwipeRefreshLayout swipeContainer;

    //
    private  MyBankCardAdapter myBankCardAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_bank_card);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);


    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.drawable.back_selector, "我的银行卡", 0);
        titlebar.setTitleBarLeftListener(this);
    }

    @Override
    protected void initView() {

        //
        swipeContainer.setOnRefreshListener(this);
        // 设置下拉圆圈上的颜色，蓝色、绿色、橙色、红色
        swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        //mSwipeLayout.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        // 设定下拉圆圈的背景
//        mSwipeLayout.setProgressBackgroundColor(R.color.red);
        // 设置圆圈的大小
        swipeContainer.setSize(SwipeRefreshLayout.LARGE);



    }

    @Override
    protected void setData() {
        if(mpreferencesUtils.isLogin(MyBankCardActivity.this,"userAccount")){
            getBankCardData();
        }else {
            startActivity(new Intent(MyBankCardActivity.this,LoginActivity.class));
            finish();
        }
    }




    @Override
    public void onRefresh() {

       getBankCardData();
    }

    /**
     * 银行卡适配器
      */

    class MyBankCardAdapter extends BaseAdapter{
        private Context context;
        private List <BankList> bankList;
       private LayoutInflater inflater;
        public  MyBankCardAdapter(Context context, List <BankList> bankList){

            this.context=context;
            this.bankList=bankList;
             inflater = LayoutInflater.from(context);

        }

        public void UpdateAdapter(List <BankList> bankList){
            this.bankList=bankList;
            notifyDataSetChanged();
        }



        @Override
        public int getCount() {
            return bankList.size();
        }

        @Override
        public Object getItem(int position) {
            return bankList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder=null;
            if(viewHolder==null){
                viewHolder=new ViewHolder();
                convertView=inflater.inflate(R.layout.bankcard_item,null);

                viewHolder.tv_card_name= (TextView) convertView.findViewById(R.id.tv_card_name);

                viewHolder.tv_card_type= (TextView) convertView.findViewById(R.id.tv_card_type);

                viewHolder.tv_card_nmber= (TextView) convertView.findViewById(R.id.tv_card_number);

                convertView.setTag(viewHolder);
            }else{
                viewHolder= (ViewHolder) convertView.getTag();
            }

            BankList info=  bankList.get(position);


            viewHolder.tv_card_name.setText(info.getBankBranch());
//            viewHolder.tv_card_type.setText(info.getBankId());
            viewHolder.tv_card_nmber.setText(info.getEncryptBankAccount());


            return convertView;
        }

        public final  class ViewHolder{
            TextView tv_card_name;
            TextView tv_card_type;
            TextView tv_card_nmber;

        }
    }






    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.titlebar_iv_back:

                finish();
            default:
        }
    }

    /**
     * 获取我的银行卡数据
     *
     */
    private void getBankCardData() {


        HttpUtils.get(Config.BANK_LIST, new HttpCallBack(MyBankCardActivity.this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                swipeContainer.setRefreshing(false);
                try {
                    if(null!=result){
                    BankCardRes Res=new BankCardRes(new JSONObject(result.toString()));
                        ArrayList<BankList> bankList = Res.getBankList();
                        if(myBankCardAdapter==null){
                            if(null!=bankList && bankList.size()>0){
                            myBankCardAdapter=new MyBankCardAdapter(MyBankCardActivity.this,bankList);
                            }else{
                                Toast.makeText(MyBankCardActivity.this, "查无数据", Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            myBankCardAdapter.UpdateAdapter(bankList);
                        }
                        listview.setAdapter(myBankCardAdapter);

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String code, String message) {

                swipeContainer.setRefreshing(false);

                Toast.makeText(MyBankCardActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
